package com.dycx.user.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dycx.user.converter.FansConverter;
import com.dycx.user.dto.FansDTO;
import com.dycx.user.entity.FansDO;
import com.dycx.user.service.FansService;
import com.dycx.user.vo.FansVO;
import com.dycx.user.mapper.FansMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * 粉丝表(Fans)表服务实现类
 *
 * @author makejava
 * @since 2024-08-07 16:38:51
 */

@Service
@AllArgsConstructor
public class FansImpl extends ServiceImpl<FansMapper, FansDO> implements FansService {

    private FansMapper fansMapper;

    /**
     * 分页查询
     *
     * @param dto 实例对象
     * @return 查询结果
     */
    @Override
    public Page<FansVO> getPage(FansDTO dto) {
        LambdaQueryWrapper<FansDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ObjectUtil.isNotNull(dto.getId()), FansDO::getId, dto.getId());
        wrapper.orderByDesc(FansDO::getId);

        Page<FansDO> entityPage = page(new Page<>(dto.getPageIndex(), dto.getPageSize()), wrapper);

        Page<FansVO> voPage = FansConverter.entityToVoPage(entityPage);
        voPage.getRecords().forEach(this::setData);

        return voPage;
    }

    /**
     * 查询详情
     *
     * @param id 主键
     * @return 查询结果
     */
    @Override
    public FansVO load(Integer id) {
        FansDO entity = getById(id);

        return setData(FansConverter.entityToVo(entity));
    }

    /**
     * 设置数据
     *
     * @param vo 实体
     * @return 查询结果
     */
    public FansVO setData(FansVO vo) {
        return vo;
    }

    /**
     * 添加
     *
     * @param dto
     * @return
     */
    @Override
    public Boolean create(FansDTO dto) {
        FansDO entity = BeanUtil.toBean(dto, FansDO.class);
        return save(entity);
    }

    /**
     * 修改
     *
     * @param dto
     * @return
     */
    @Override
    public Boolean edit(FansDTO dto) {
        FansDO entity = BeanUtil.toBean(dto, FansDO.class);
        return updateById(entity);
    }

    /**
     * 删除
     *
     * @param id 主键
     * @return
     */
    @Override
    public Boolean delete(Integer id) {
        return removeById(id);
    }

    /**
     * 获取最大排序
     *
     * @return 查询结果
     */
    @Override
    public Integer getMaxSort() {
        return fansMapper.getMaxSort();
    }
}

